package com.yoofn.sensing.mvp.adapter


import android.databinding.BaseObservable
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.yoofn.kotlin.base.BaseFragment

/**
 * Created by kalshen on 17/4/24.

 * @描述 &{TODO}
 */
class MultiTypeAdapter(val fragment: BaseFragment<out ViewDataBinding, out Any>, val recyclerView: RecyclerView) : RecyclerView.Adapter<MultiTypeAdapter.ItemViewHolder>() {


    abstract class Item : BaseObservable() {
        var position: Int = 0
        var list: List<Item>? = null
        var fragment: BaseFragment<out ViewDataBinding, out Any>? = null
        var recyclerView: RecyclerView? = null
        var rootView: View? = null
        var size: Int = 0

        abstract val type: Int

        open fun initView() {

        }
    }


    private val items = ArrayList<Item>()
    private var tag: Any? = null
    private var tag2: Any? = null
    ///////////////////////////////////////////////////////

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        return ItemViewHolder.create(parent, viewType)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val item = items[position]
        item.position = position
        item.size = itemCount
        item.list = items
        item.recyclerView = recyclerView
        holder.bindTo(item, fragment)
    }

    override fun getItemCount(): Int {
        return items.size
    }

    override fun getItemViewType(position: Int): Int {
        return items[position].type
    }

    ///////////////////////////////////////////////////////
    // operate items

    fun getItems(): List<Item> {
        return items
    }

    fun getItemsOf(start: Int, end: Int): List<Item> {
        return items.subList(start, end + 1)
    }


    fun setItem(item: Item) {
        clearItems()
        addItem(item)
    }

    fun setItems(items: List<Item>) {
        clearItems()
        addItems(items)
    }

    fun setTag(obj: Any) {
        tag = obj
    }

    fun getTag(): Any? {
        return tag
    }

    fun setTag2(obj2: Any) {
        tag2 = obj2
    }

    fun getTag2(): Any? {
        return tag2
    }


    fun getItem(position: Int): Item {
        return items[position]
    }

    fun hasItem(item: Item): Boolean {
        return items.contains(item)
    }

    fun addItem(item: Item) {
        items.add(item)
    }

    fun addItem(item: Item, index: Int) {
        items.add(index, item)
    }

    fun addItems(items: List<Item>) {
        this.items.addAll(items)
    }

    fun removeItem(item: Item) {
        items.remove(item)
    }

    fun removeItems(start: Int, end: Int) {
        (start..end).forEach { items.removeAt(start) }


    }

    fun clearItems() {
        items.clear()
    }

    ///////////////////////////////////////////////////
    class ItemViewHolder(private val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bindTo(item: Item, fragment: BaseFragment<out ViewDataBinding, out Any>) {
//            binding.setVariable(BR.item, item)
            item.rootView = binding.root
            item.fragment = fragment
            binding.executePendingBindings()
            item.initView()
        }

        companion object {

            fun create(parent: ViewGroup, viewType: Int): ItemViewHolder {
                val binding = DataBindingUtil.inflate<ViewDataBinding>(LayoutInflater.from(parent.context),
                        viewType, parent, false)
                return ItemViewHolder(binding)
            }
        }
    }
}